package com.sj.web.base.utils;

import com.sj.base.enums.GlobalEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import com.sj.web.base.vo.ResultVO;


/**
 * @author swallow
 * Created by swallow on 2018/9/7.<br>
 */
public class ResponseBuilderUtil {

    private static final Logger logger = LoggerFactory.getLogger(ResponseBuilderUtil.class);

    /**
     * 构建成功返回的json
     * @return 返回构建对象
     */
    public static ResultVO buildSuccessJson(){
        return buildSuccessJson(null);
    }

    /**
     * 构建成功返回的json 携带参数
     * @param data 参数
     * @return 返回构建对象
     */
    public static ResultVO buildSuccessJson(Object data){
        logger.debug("[ResponseBuilderUtil][buildSuccessJson] entering data:{}",data);
        ResultVO resultVO = new ResultVO();
        resultVO.setResult(ResultVO.RESULT_SUCCESS);
        if(data!=null){
            resultVO.setData(data);
        }
        logger.info("[ResponseBuilderUtil][buildSuccessJson] 构建成功返回的json 携带参数 data:{},resultVO:{}",data,resultVO);
        return resultVO;
    }

    /**
     * 构建错误json
     * 构建
     * @return
     */
    public static ResultVO buildErrorJSON(){
        return buildGlobalJSON(GlobalEnum.SYSTEM_ERROR,"",ResultVO.RESULT_FAILED);
    }

    /**
     * 构建错误json
     * @return
     */
    public static ResultVO buildErrorJSON(GlobalEnum globalEnum){
        return buildGlobalJSON(globalEnum,"",ResultVO.RESULT_FAILED);
    }

    /**
     * 构建错误json
     * @return
     */
    public static ResultVO buildErrorJSON(GlobalEnum globalEnum,String message){
        return buildGlobalJSON(globalEnum,message,ResultVO.RESULT_FAILED);
    }

    /**
     * 构建正确访问的json
     * @param globalEnum
     * @return
     */
    public static ResultVO buildSuccessJson(GlobalEnum globalEnum){
        return buildGlobalJSON(globalEnum,"",ResultVO.RESULT_SUCCESS);
    }

    /**
     * 构建返回json
     * @return
     */
    public static ResultVO buildGlobalJSON(GlobalEnum globalEnum, String message, int result){
        logger.debug("[ResponseBuilderUtil][buildGlobalJSON] entering globalEnum:{},message:{},result:{}", globalEnum, message, result);
        ResultVO resultVO = new ResultVO();
        resultVO.setResult(result);
        resultVO.setErrorcode(globalEnum.getCode());
        if(StringUtils.isNotBlank(message)){
            resultVO.setMessage(message);
        }else{
            resultVO.setMessage(globalEnum.getMessage());
        }
        logger.info("[ResponseBuilderUtil][buildGlobalJSON] desc globalEnum:{},message:{},resultVO:{},result:{}", globalEnum, message,resultVO,result);
        return resultVO;
    }

    /**
     * 构建返回 jsonView返回的 modelAdminView
     * @param resultVO 封装对象
     * @return 返回json对应的modelAdminView对象
     */
    public static ModelAndView buildJsonView(ResultVO resultVO) {
        ModelAndView modelAndView = new ModelAndView("jsonview");
        modelAndView.addObject("resultVO",resultVO);
        return modelAndView;
    }
}
